﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;

namespace QuanLyBanHang
{
    public partial class F830_Tra_cuu_danh_muc_loai_san_pham : Form
    {
        public F830_Tra_cuu_danh_muc_loai_san_pham()
        {
            InitializeComponent();
            set_define_events();
        }

        #region Members
        BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
        #endregion

        #region Private Methods

        private void set_define_events()
        {
            this.Load += F830_Tra_cuu_danh_muc_loai_san_pham_Load;
            this.m_grv_loai_san_pham.Click += m_grv_loai_san_pham_Click;
            this.m_txt_tim_kiem_loai_san_pham.TextChanged += m_txt_tim_kiem_loai_san_pham_TextChanged;
        }

        private void load_data_to_grid()
        {
            var v_lst_danh_muc_loai_san_pham = db.DM_LOAI_SAN_PHAM
                .Where(x => x.MA_LOAI_SAN_PHAM.Contains(m_txt_tim_kiem_loai_san_pham.Text.Trim())
                || x.TEN_LOAI_SAN_PHAM.Contains(m_txt_tim_kiem_loai_san_pham.Text.Trim())
                || x.DAC_DIEM.Contains(m_txt_tim_kiem_loai_san_pham.Text.Trim())
                || m_txt_tim_kiem_loai_san_pham.Text == "")
                .OrderBy(x => x.MA_LOAI_SAN_PHAM)
                .Select(x => new
                {
                    ID = x.ID,
                    MA_LOAI_SAN_PHAM = x.MA_LOAI_SAN_PHAM,
                    TEN_LOAI_SAN_PHAM = x.TEN_LOAI_SAN_PHAM,
                    DAC_DIEM = x.DAC_DIEM
                })
                .ToList();
            m_grv_loai_san_pham.DataSource = v_lst_danh_muc_loai_san_pham;
            m_grv_loai_san_pham.Columns[0].HeaderText = "ID";
            m_grv_loai_san_pham.Columns[0].Visible = false;
            m_grv_loai_san_pham.Columns[0].Width = 20;
            m_grv_loai_san_pham.Columns[1].HeaderText = "Mã loại sản phẩm";
            m_grv_loai_san_pham.Columns[1].Width = 100;
            m_grv_loai_san_pham.Columns[2].HeaderText = "Tên loại sản phẩm";
            m_grv_loai_san_pham.Columns[2].Width = 100;
            m_grv_loai_san_pham.Columns[3].HeaderText = "Đặc điểm";
            m_grv_loai_san_pham.Columns[3].Width = 300;
        }
        
        #endregion
        private void F830_Tra_cuu_danh_muc_loai_san_pham_Load(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        void m_txt_tim_kiem_loai_san_pham_TextChanged(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        void m_grv_loai_san_pham_Click(object sender, EventArgs e)
        {
            try
            {
                decimal id_loai_sp = (decimal)m_grv_loai_san_pham.CurrentRow.Cells[0].Value; //Xac dinh id loai san pham
                DM_LOAI_SAN_PHAM v_dm_loai_sp = db.DM_LOAI_SAN_PHAM.FirstOrDefault(x => x.ID == id_loai_sp);
                if (v_dm_loai_sp != null)
                {
                    m_txt_ma_loai_san_pham.Text = v_dm_loai_sp.MA_LOAI_SAN_PHAM;
                    m_txt_ten_loai_san_pham.Text = v_dm_loai_sp.TEN_LOAI_SAN_PHAM;
                    m_txt_dac_diem.Text = v_dm_loai_sp.DAC_DIEM;
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_quay_lai_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }
    }
}
